package com.lemon.tuvi2015;

import org.json.JSONException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lemon.tuvi2015.object.DetailsObject;
import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;

@SuppressLint("NewApi") public class DetailsActivity extends Activity {
	String title, content;
	CustomActionbar actionbar;
	WebView webView;
	GlobalObject Object;
	DetailsObject detailsObject;
	ProgressBar progressBar;
	TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.congiap_details);
		initView();
		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.OBJ))
			Object = (GlobalObject) extras.getSerializable(TagName.OBJ);
		if (getIntent().hasExtra(TagName.TITLE))
			title = extras.getString(TagName.TITLE);
		if (getIntent().hasExtra(TagName.KEY)){
			content = extras.getString(TagName.KEY);
			showContent(content);
		}
		
		initActionbar();
	}

	void initView() {
		textView=(TextView) findViewById(R.id.textView);
		progressBar=(ProgressBar) findViewById(R.id.progressBar);
		webView = (WebView) findViewById(R.id.webView);
		webView.setWebViewClient(new WebViewClient() {
			public void onPageFinished(WebView view, String url) {
				webView.setVisibility(View.VISIBLE);
				progressBar.setVisibility(View.GONE);
			}
		});
		webView.setBackgroundColor(Color.TRANSPARENT);
//		webView.setBackgroundColor(Color.parseColor("#919191"));
	    if (Build.VERSION.SDK_INT >= 11) {
	    	webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
	    }
		webView.setInitialScale(100);
//		webView.getSettings().setLoadWithOverviewMode(true);
//		webView.getSettings().setUseWideViewPort(true);
//		webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		webView.setScrollbarFadingEnabled(false);
		webView.setPadding(10, 10, 10, 10);
		webView.getSettings().setBuiltInZoomControls(true);

	}

	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		if (title != null && !title.equals(""))
			actionbar.setActionbar(title, listenerBack, null);
		else
			actionbar.setActionbar("Tử vi 2015", listenerBack, null);
	}

	private void showContent(String content) {
		try {
				if(!JsonParser.readStatus(content).equals("")){
					if(JsonParser.readNotRuoi(content)!=null &&JsonParser.readNotRuoi(content).size()>0){
						final String mimeType = "text/html";
						final String encoding = "utf-8";
//						Log.e("showcontent", content);
//						String data = Html.fromHtml(JsonParser.readNotRuoi(content).get(0).getcontent()).toString();
						String data=JsonParser.readNotRuoi(content).get(0).getcontent();
						webView.loadDataWithBaseURL(null, data, mimeType, encoding, null);
					}else
						showErr();
			}
		} catch (JSONException e) {
			e.printStackTrace();
			showErr();
		}
	}
	
	void showErr(){
		String err="Dữ liệu đang cập nhật!";
		progressBar.setVisibility(View.GONE);
		textView.setText(err);
	}
}
